package com.ycq.cloud.helloserver.Controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class IndexController {

    @Autowired
    private RestTemplate restTemplate;


//    在@HystrixCommand添加commandProperties = @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")
//    配置说明
//    /index请求和indexFallback方法是在同一个线程中，execution.isolation.strategy的默认值为THREAD，
//    表示当/index请求失败时会新开一个线程调用indexFallback方法即处于不同线程下
    @HystrixCommand(fallbackMethod = "indexFallback",commandProperties = @HystrixProperty(name="execution.isolation.strategy",value = "SEMAPHORE"))
    @GetMapping("/index")
    public String index(){
        System.out.println("是哪个呢？");
        return restTemplate.getForEntity("http://helloclient/hello",String.class).getBody();
    }


    public String indexFallback(){
        return "断路器生效";
    }

}
